package com.xxhui.viewgroupdemo.View;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

/**
 * Created by hui on 2016/7/14.
 */
public class MyScrollView extends ViewGroup {

    private int screenHeight;
    private Scroller scroller;
    private int mLastY;
    private int mStart;
    private int mEnd;

    public MyScrollView(Context context) {
        super(context);
        initParams(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initParams(context);
    }

    private void initParams(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        screenHeight = metrics.heightPixels;
        scroller = new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean b, int l, int t, int r, int bo) {
        int childCount = getChildCount();
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        // 设置ViewGroup的高度
        mlp.height = screenHeight * childCount;
        setLayoutParams(mlp);
        for (int j = 0; j < childCount; j++) {
            View child = getChildAt(j);
            if (child.getVisibility() != View.GONE) {
                child.layout(l, j * screenHeight, r, (j + 1) * screenHeight);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();//y完全可以用event.getY()代替，难道是优化么...
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = y;
                mStart = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!scroller.isFinished()) {
                    scroller.abortAnimation();
                }
                int distanceY = mLastY - y;
                if (getScaleY() < 0) {
                    distanceY = 0;
                }
                if (getScaleY() > getHeight() - screenHeight) {
                    distanceY = 0;
                }
                scrollBy(0, distanceY);//代码实现使用了scrollTo方法
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                int dScrollY = checkAlignment();
                if (dScrollY > 0) {
                    if (dScrollY < screenHeight / 3) {
                        scroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY);
                    } else {
                        scroller.startScroll(
                                0, getScrollY(),
                                0, screenHeight - dScrollY);
                    }
                } else {
                    if (-dScrollY < screenHeight / 3) {
                        scroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY);
                    } else {
                        scroller.startScroll(
                                0, getScrollY(),
                                0, -screenHeight - dScrollY);
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    private int checkAlignment() {
        int mEnd = getScrollY();
        boolean isUp = ((mEnd - mStart) > 0) ? true : false;
        int lastPrev = mEnd % screenHeight;
        int lastNext = screenHeight - lastPrev;
        if (isUp) {
            //向上的
            return lastPrev;
        } else {
            return -lastNext;
        }
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(scroller.computeScrollOffset()){
            scrollTo(0,scroller.getCurrY());
        }
    }
}
